package com.dsz.du.mealbooking.contoller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dsz.du.mealbooking.model.MealModel;
import com.dsz.du.mealbooking.model.UserModel;
import com.dsz.du.mealbooking.services.BookMealServices;
import com.dsz.du.mealbooking.services.UserService;

/**
 * 业务页面控制器
 * 
 * @author du
 * 
 */
@Controller
public class MealBookingController {

	public MealBookingController() {
	}

	private static Logger logger = Logger.getLogger(MealBookingController.class);

	@Autowired
	private BookMealServices bookMealServices;

	@Autowired
	private UserService userServices;
	
	@RequestMapping("/addMeal.do")
	protected String addMeal(ModelMap model, String userId, String mealId)
			throws Exception {

		if (logger.isDebugEnabled()) {
			logger.debug("addMeal userId is : " + userId);
			logger.debug("addMeal mealId is : " + mealId);
		}

		UserModel userModel = userServices.getUserById(userId);
		List<MealModel> mealList = userModel.getMeals();
		
		MealModel meal = new MealModel();
		meal.setMealId(mealId);
		mealList.add(meal);
		
		bookMealServices.addMeal(userModel);
		
		List<MealModel> meals = bookMealServices.getOrder(userId);
		model.addAttribute("meals", meals);
		
		return "orderDetail";

	}

	@RequestMapping("/deleteMeal.do")
	protected String deleteMeal(ModelMap model, String userId, String mealIds)
			throws Exception {

		if (logger.isDebugEnabled()) {
			logger.debug("delete userId is : " + userId);
			logger.debug("delete mealId is : " + mealIds);
		}

		if (mealIds != null) {
			bookMealServices.deleteMeal(userId, mealIds);
		}

		List<MealModel> meals = bookMealServices.getOrder(userId);
		model.addAttribute("meals", meals);
		return "orderDetail";

	}
}
